package com.example.demo.blog.service;

import com.baomidou.mybatisplus.solon.plugins.pagination.Page;
import com.example.demo.blog.dto.BlogDTO;
import com.example.demo.blog.dto.req.BlogReq;
import com.example.demo.blog.dto.req.BlogSearchReq;
import com.example.demo.blog.mapper.BlogMapper;
import com.example.demo.blog.model.Blog;
import com.example.demo.core.web.core.model.PageReq;
import com.example.demo.core.web.core.service.DemoBaseService;
import com.example.demo.core.web.core.utils.converter.ConverterUtils;
import java.util.List;
import org.noear.solon.annotation.Component;

/**
 * 博文 服务类
 *
 * @author Airhead
 */
@Component
public class BlogService extends DemoBaseService<BlogMapper, Blog> {
  public List<BlogDTO> list(BlogSearchReq searchReq) {
    return baseMapper.list(searchReq);
  }

  public Page<BlogDTO> paginate(BlogSearchReq searchReq, PageReq pageReq) {
    return baseMapper.paginate(pageReq.buildPage(), searchReq);
  }

  public Boolean add(BlogReq req) {
    Blog blog = ConverterUtils.map(req, Blog.class);
    return save(blog);
  }

  public Boolean update(BlogReq req) {
    Blog blog = ConverterUtils.map(req, Blog.class);
    return updateById(blog);
  }

  public Boolean delete(List<Integer> idList) {
    return removeByIds(idList);
  }

  public BlogDTO get(Integer id) {
    Blog blog = getById(id);
    if (blog == null) {
      return null;
    }

    return ConverterUtils.map(blog, BlogDTO.class);
  }
}
